//
//  PEPullTableView.m
//  PullTable
//
//  Created by hemingway payegis on 13-1-6.
//  Copyright (c) 2013年 payegis. All rights reserved.
//

#import "PETableView.h"
#import "EGORefreshTableHeaderView.h"
#import "PETableMoreView.h"

@interface PETableView ()
<EGORefreshTableHeaderDelegate, PETableMoreViewDelegate>
{
    EGORefreshTableHeaderView *_refreshHeaderView;
    BOOL _reloading;
    
    PETableMoreView* _moreView;
    BOOL _loadingMoreData;
}
- (void)reloadTableViewDataSource;
- (void)loadTableViewMoreData;
@end


@implementation PETableView
@synthesize enabledPullRefresh = _enabledPullRefresh;
@synthesize enabledLoadMoreData = _enabledLoadMoreData;

- (void)dealloc
{
    [_moreView release];
    [super dealloc];
}

- (void)setEnabledPullRefresh:(BOOL)enabledPullRefresh
{
    _enabledPullRefresh = enabledPullRefresh;
    if (enabledPullRefresh)
    {
        if (nil == _refreshHeaderView)
        {
            EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.bounds.size.height, self.frame.size.width, self.bounds.size.height)];
            view.backgroundColor = [UIColor clearColor];
            view.delegate = self;
            [self insertSubview:view atIndex:0];
            _refreshHeaderView = view;
            [view release];
        }
    }
    else
    {
        [_refreshHeaderView removeFromSuperview];
    }
}

#pragma mark public methods

- (void)didScroll
{
    [_refreshHeaderView egoRefreshScrollViewDidScroll:self];
}

- (void)didEndDragging
{
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:self];
}

- (void)setTableViewIsReloading
{
    _reloading = YES;
    self.contentInset = UIEdgeInsetsMake(60, 0, 0, 0);
    self.contentOffset = CGPointMake(0,-65);
    [_refreshHeaderView setState:EGOOPullRefreshLoading];
}

- (BOOL)isTableViewReloading;
{
    return _reloading;
}

- (void)reloadTableViewDataDone
{
	//  model should call this when its done loading
	_reloading = NO;
	[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self];
}

- (void)loadTableViewMoreDataDone
{
    _loadingMoreData = NO;
    [_moreView tableMoreViewDidFinishedLoading];
}

- (void)showTableMoreView
{
    if (_enabledLoadMoreData)
    {
        if (nil == _moreView)
        {
            _moreView = [[PETableMoreView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 50)];
            _moreView.delegate = self;
//            self.tableFooterView = _moreView;
        }
//        _moreView.hidden = NO;
        self.tableFooterView = _moreView;
    }
}

- (void)hideTableMoreView
{
    self.tableFooterView = nil;

//    [_moreView tableMoreViewDidFinishedLoading];
//    _moreView.hidden = YES;
}

#pragma mark private methods

- (void)reloadTableViewDataSource
{
    //取消获取更多
    [self loadTableViewMoreDataDone];
    
	_reloading = YES;
    if (self.delegate && [self.delegate respondsToSelector:@selector(didReloadTableViewDataSource:)])
    {
        [self.delegate performSelector:@selector(didReloadTableViewDataSource:) withObject:self];
    }
}

- (void)loadTableViewMoreData
{
    //取消下拉刷新
    [self reloadTableViewDataDone];
    
    _loadingMoreData = YES;
    if (self.delegate && [self.delegate respondsToSelector:@selector(didLoadTableViewMoreData:)])
    {
        [self.delegate performSelector:@selector(didLoadTableViewMoreData:) withObject:self];
    }
}

#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view
{
	[self reloadTableViewDataSource];
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view
{
	return _reloading; // should return if data source model is reloading
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view
{
	return [NSDate date]; // should return date data source was last changed
}

#pragma mark PETableMoreViewDelegate

- (void)tableMoreDataIsLoading:(PETableMoreView*)tableMoreView
{
    [self loadTableViewMoreData];
}
@end
